import { BrowserWindow } from 'electron'
import { WinOptionsTy } from '../types/type'
import { join } from 'path'
import WinMap from '../Root/WinMap'
import { is } from '@electron-toolkit/utils'
import icon from '../../resources/record.png?asset'
class CreateWin {
  win: BrowserWindow
  constructor(options: WinOptionsTy, preload: string) {
    this.win = new BrowserWindow({
      ...options,
      show: false,
      frame: false,
      center: true,
      transparent: true,
      icon,
      webPreferences: {
        preload: join(__dirname, preload),
        sandbox: false
      }
    })

    this.win.on('ready-to-show', () => {
      this.win.show()
    })

    WinMap.set(this.win.id, this.win)

    this.win.on('close', () => {
      WinMap.delete(this.win.id)
      this.win.destroy()
    })
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      this.win.loadURL(process.env['ELECTRON_RENDERER_URL'])
    } else {
      this.win.loadFile(join(__dirname, '../renderer/index.html'))
    }
  }

  getWinVm() {
    return this.win
  }

  getWinVmProps(key: string) {
    return this.win[key]
  }
}

export default CreateWin
